﻿namespace KeRui.Common
{
    using System;
    using System.IO;
    using System.Text;

    public class FileObj : IDisposable
    {
        private bool _alreadyDispose = false;

        public static void CopyDir(string srcPath, string aimPath)
        {
            try
            {
                if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                {
                    aimPath = aimPath + Path.DirectorySeparatorChar;
                }
                if (!Directory.Exists(aimPath))
                {
                    Directory.CreateDirectory(aimPath);
                }
                string[] fileSystemEntries = Directory.GetFileSystemEntries(srcPath);
                foreach (string str in fileSystemEntries)
                {
                    if (Directory.Exists(str))
                    {
                        CopyDir(str, aimPath + Path.GetFileName(str));
                    }
                    else
                    {
                        File.Copy(str, aimPath + Path.GetFileName(str), true);
                    }
                }
            }
            catch (Exception exception)
            {
                throw new Exception(exception.ToString());
            }
        }

        public static void DeleteFolder(string dir)
        {
            if (Directory.Exists(dir))
            {
                foreach (string str in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(str))
                    {
                        File.Delete(str);
                    }
                    else
                    {
                        DeleteFolder(str);
                    }
                }
                Directory.Delete(dir);
            }
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool isDisposing)
        {
            if (!this._alreadyDispose)
            {
                this._alreadyDispose = true;
            }
        }

        public static void FileAdd(string Path, string strings)
        {
            StreamWriter writer = File.AppendText(Path);
            writer.Write(strings);
            writer.Flush();
            writer.Close();
        }

        public static void FileCoppy(string orignFile, string NewFile)
        {
            File.Copy(orignFile, NewFile, true);
        }

        public static void FileDel(string Path)
        {
            File.Delete(Path);
        }

        public static void FileMove(string orignFile, string NewFile)
        {
            File.Move(orignFile, NewFile);
        }

        ~FileObj()
        {
            this.Dispose();
        }

        public static void FolderCreate(string orignFolder, string NewFloder)
        {
            Directory.SetCurrentDirectory(orignFolder);
            Directory.CreateDirectory(NewFloder);
        }

        public static string GetPostfixStr(string filename)
        {
            int startIndex = filename.LastIndexOf(".");
            int length = filename.Length;
            return filename.Substring(startIndex, length - startIndex);
        }

        public static string ReadFile(string Path)
        {
            string str = "";
            if (!File.Exists(Path))
            {
                return "";
            }
            StreamReader reader = new StreamReader(Path, Encoding.GetEncoding("gb2312"));
            str = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            return str;
        }

        public static string ReadFile(string Path, string StrCode)
        {
            string str = "";
            if (!File.Exists(Path))
            {
                return "";
            }
            StreamReader reader = new StreamReader(Path, Encoding.GetEncoding(StrCode));
            str = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            return str;
        }

        public static void WriteFile(string Path, string Strings)
        {
            if (!File.Exists(Path))
            {
                File.Create(Path).Close();
            }
            StreamWriter writer = new StreamWriter(Path, false, Encoding.GetEncoding("gb2312"));
            writer.Write(Strings);
            writer.Close();
            writer.Dispose();
        }

        public static void WriteFile(string Path, string Strings, string StrCode)
        {
            if (!File.Exists(Path))
            {
                File.Create(Path).Close();
            }
            StreamWriter writer = new StreamWriter(Path, false, Encoding.GetEncoding(StrCode));
            writer.Write(Strings);
            writer.Close();
            writer.Dispose();
        }

        public static void WriteFileNoSign(string Path, string Strings)
        {
            if (!File.Exists(Path))
            {
                File.Create(Path).Close();
            }
            UTF8Encoding encoding = new UTF8Encoding(false);
            StreamWriter writer = new StreamWriter(Path, false, encoding);
            writer.Write(Strings);
            writer.Close();
            writer.Dispose();
        }
    }
}

